#ifndef __MODELEDITOR_RESDIR_H
#define __MODELEDITOR_RESDIR_H

#include "../../JuceLibraryCode/JuceHeader.h"
#include "TextureViewDialog.h"
//==============================================================================
class ModelEditorMainFrame;
//==============================================================================
class ModelAndTextureFileFilter : public FileFilter
{
public:
	ModelAndTextureFileFilter(const String &filterDescription) : FileFilter(filterDescription)
	{
	}
	~ModelAndTextureFileFilter() {}

	bool isFileSuitable(const File &file) const;

	bool isDirectorySuitable(const File &file) const
	{
		return true;
	}
};


class ResourceDirComponent  :	public Component,
								public ButtonListener,
								public FileBrowserListener
{
public:
    ResourceDirComponent(ModelEditorMainFrame *mainFrame) : thread ("Resource tree thread"), pMainFrame(mainFrame)
	{
		setName("Resource tree Views");

		filter = new ModelAndTextureFileFilter("AllSupportFile");

		File folder(File::getSpecialLocation(File::currentApplicationFile));
		if( folder.getParentDirectory() != folder )
			folder = folder.getParentDirectory();
		if( folder.getParentDirectory() != folder )
			folder = folder.getParentDirectory();


        directoryList = new DirectoryContentsList(filter, thread);
		directoryList->setIgnoresHiddenFiles(true);
        directoryList->setDirectory(folder, true, true);
        thread.startThread(3);

		addAndMakeVisible(fileTreeComp = new FileTreeComponent(*directoryList));
		//fileTreeComp->setRootItemVisible(true);
		fileTreeComp->addListener(this);


		addAndMakeVisible(model_filter[0] = new TextButton("md2"));
        model_filter[0]->setClickingTogglesState(true);
		model_filter[0]->setToggleState(CModelEditorConfig::GetInstance()->m_ResDirFileFilter.bShowMD2, false);
        model_filter[0]->setColour(TextButton::buttonColourId, Colours::white);
        model_filter[0]->setColour(TextButton::buttonOnColourId, Colours::blueviolet.brighter());
		model_filter[0]->addListener(this);
		addAndMakeVisible(model_filter[1] = new TextButton("md3"));
        model_filter[1]->setClickingTogglesState(true);
		model_filter[1]->setToggleState(CModelEditorConfig::GetInstance()->m_ResDirFileFilter.bShowMD3, false);
        model_filter[1]->setColour(TextButton::buttonColourId, Colours::white);
        model_filter[1]->setColour(TextButton::buttonOnColourId, Colours::blueviolet.brighter());
		model_filter[1]->addListener(this);		
		addAndMakeVisible(model_filter[2] = new TextButton("ms3d"));
        model_filter[2]->setClickingTogglesState(true);
		model_filter[2]->setToggleState(CModelEditorConfig::GetInstance()->m_ResDirFileFilter.bShowMS3D, false);
        model_filter[2]->setColour(TextButton::buttonColourId, Colours::white);
        model_filter[2]->setColour(TextButton::buttonOnColourId, Colours::blueviolet.brighter());
		model_filter[2]->addListener(this);
		addAndMakeVisible(model_filter[3] = new TextButton("btm1"));
        model_filter[3]->setClickingTogglesState(true);
		model_filter[3]->setToggleState(CModelEditorConfig::GetInstance()->m_ResDirFileFilter.bShowBTM1, false);
        model_filter[3]->setColour(TextButton::buttonColourId, Colours::white);
        model_filter[3]->setColour(TextButton::buttonOnColourId, Colours::blueviolet.brighter());
		model_filter[3]->addListener(this);

		addAndMakeVisible(texture_switch = new TextButton("show texture"));
        texture_switch->setClickingTogglesState(true);
		texture_switch->setToggleState(CModelEditorConfig::GetInstance()->m_ResDirFileFilter.bShowPicture, false);
        texture_switch->setColour(TextButton::buttonColourId, Colours::white);
        texture_switch->setColour(TextButton::buttonOnColourId, Colours::blueviolet.brighter());
		texture_switch->addListener(this);

		if( CModelEditorConfig::GetInstance()->m_ResDirFileFilter.bEnableHardDiskVolume[0] )
		{
			addAndMakeVisible(volume_filter[0] = new TextButton("C"));

			volume_filter[0]->setToggleState(false, false);
			volume_filter[0]->setRadioGroupId(1234);
			volume_filter[0]->setColour(TextButton::buttonColourId, Colours::white);
			volume_filter[0]->setColour(TextButton::buttonOnColourId, Colours::blueviolet.brighter());
			volume_filter[0]->addListener(this);
		}
		if( CModelEditorConfig::GetInstance()->m_ResDirFileFilter.bEnableHardDiskVolume[1] )
		{
			addAndMakeVisible(volume_filter[1] = new TextButton("D"));

			volume_filter[1]->setToggleState(false, false);
			volume_filter[1]->setRadioGroupId(1234);
			volume_filter[1]->setColour(TextButton::buttonColourId, Colours::white);
			volume_filter[1]->setColour(TextButton::buttonOnColourId, Colours::blueviolet.brighter());
			volume_filter[1]->addListener(this);
		}
		if( CModelEditorConfig::GetInstance()->m_ResDirFileFilter.bEnableHardDiskVolume[2] )
		{
			addAndMakeVisible(volume_filter[2] = new TextButton("E"));

			volume_filter[2]->setToggleState(false, false);
			volume_filter[2]->setRadioGroupId(1234);
			volume_filter[2]->setColour(TextButton::buttonColourId, Colours::white);
			volume_filter[2]->setColour(TextButton::buttonOnColourId, Colours::blueviolet.brighter());
			volume_filter[2]->addListener(this);
		}
		if( CModelEditorConfig::GetInstance()->m_ResDirFileFilter.bEnableHardDiskVolume[3] )
		{
			addAndMakeVisible(volume_filter[3] = new TextButton("F"));

			volume_filter[3]->setToggleState(false, false);
			volume_filter[3]->setRadioGroupId(1234);
			volume_filter[3]->setColour(TextButton::buttonColourId, Colours::white);
			volume_filter[3]->setColour(TextButton::buttonOnColourId, Colours::blueviolet.brighter());
			volume_filter[3]->addListener(this);
		}

		addAndMakeVisible(volume_filter[4] = new TextButton("WorkDir"));

		volume_filter[4]->setRadioGroupId(1234);
		volume_filter[4]->setToggleState(true, false);
		volume_filter[4]->setColour(TextButton::buttonColourId, Colours::white);
		volume_filter[4]->setColour(TextButton::buttonOnColourId, Colours::blueviolet.brighter());
		volume_filter[4]->addListener(this);
	}

	~ResourceDirComponent() 
	{
		fileTreeComp = nullptr;
		// (need to make sure this is deleted before the TimeSliceThread)
		directoryList = nullptr;
	}

	void paint(Graphics& g)
    {
        g.setColour(Colours::lightgrey);


        if(fileTreeComp != nullptr)
			g.drawRect(	fileTreeComp->getX(), fileTreeComp->getY(),
						fileTreeComp->getWidth(), fileTreeComp->getHeight() );
    }

    void resized()
    {
		texture_switch->setBounds(20, 0, 180, 25);
		for( int i=0; i<4; i++ )
		{
			model_filter[i]->setBounds(i * 55, 30, 55, 25);
			model_filter[i]->setConnectedEdges(((i != 0) ? Button::ConnectedOnLeft : 0) | ((i != 3) ? Button::ConnectedOnRight : 0));
		}
		volume_filter[4]->setBounds(0, 60, 75, 25);
		for( int j=0; j<4; j++ )
		{
			if( volume_filter[j] )
				volume_filter[j]->setBounds(75 + j * 35, 60, 35, 25);
		}


        fileTreeComp->setBounds(0, 100, getWidth(), getHeight() - 100);
    }

	void buttonClicked(Button* buttonThatWasClicked)
    {
		if( buttonThatWasClicked == texture_switch )
		{
			CModelEditorConfig::GetInstance()->m_ResDirFileFilter.bShowPicture = !CModelEditorConfig::GetInstance()->m_ResDirFileFilter.bShowPicture;
			directoryList->refresh();
		}
		else if( buttonThatWasClicked == model_filter[0] )
		{
			CModelEditorConfig::GetInstance()->m_ResDirFileFilter.bShowMD2 = !CModelEditorConfig::GetInstance()->m_ResDirFileFilter.bShowMD2;
			directoryList->refresh();
		}
		else if( buttonThatWasClicked == model_filter[1] )
		{
			CModelEditorConfig::GetInstance()->m_ResDirFileFilter.bShowMD3 = !CModelEditorConfig::GetInstance()->m_ResDirFileFilter.bShowMD3;
			directoryList->refresh();
		}
		else if( buttonThatWasClicked == model_filter[2] )
		{
			CModelEditorConfig::GetInstance()->m_ResDirFileFilter.bShowMS3D = !CModelEditorConfig::GetInstance()->m_ResDirFileFilter.bShowMS3D;
			directoryList->refresh();
		}
		else if( buttonThatWasClicked == model_filter[3] )
		{
			CModelEditorConfig::GetInstance()->m_ResDirFileFilter.bShowBTM1 = !CModelEditorConfig::GetInstance()->m_ResDirFileFilter.bShowBTM1;
			directoryList->refresh();
		}
		else if( buttonThatWasClicked == volume_filter[4] )
		{
			volume_filter[4]->setToggleState(true, false);
			if( volume_filter[0] )
				volume_filter[0]->setToggleState(false, false);
			if( volume_filter[1] )
				volume_filter[1]->setToggleState(false, false);
			if( volume_filter[2] )
				volume_filter[2]->setToggleState(false, false);
			if( volume_filter[3] )
				volume_filter[3]->setToggleState(false, false);

			File folder(File::getSpecialLocation(File::currentApplicationFile));
			if( folder.getParentDirectory() != folder )
				folder = folder.getParentDirectory();
			if( folder.getParentDirectory() != folder )
				folder = folder.getParentDirectory();			
			directoryList->setDirectory(folder, true, true);
			directoryList->refresh();
		}
		else if( buttonThatWasClicked == volume_filter[0] )
		{
			volume_filter[0]->setToggleState(true, false);
			volume_filter[4]->setToggleState(false, false);
			if( volume_filter[1] )
				volume_filter[1]->setToggleState(false, false);
			if( volume_filter[2] )
				volume_filter[2]->setToggleState(false, false);
			if( volume_filter[3] )
				volume_filter[3]->setToggleState(false, false);
			File folder("c:");
			directoryList->setDirectory(folder, true, true);
			directoryList->refresh();

		}
		else if( buttonThatWasClicked == volume_filter[1] )
		{
			volume_filter[1]->setToggleState(true, false);
			volume_filter[4]->setToggleState(false, false);
			if( volume_filter[0] )
				volume_filter[0]->setToggleState(false, false);
			if( volume_filter[2] )
				volume_filter[2]->setToggleState(false, false);
			if( volume_filter[3] )
				volume_filter[3]->setToggleState(false, false);
			File folder("d:");
			directoryList->setDirectory(folder, true, true);
			directoryList->refresh();
		}
		else if( buttonThatWasClicked == volume_filter[2] )
		{
			volume_filter[2]->setToggleState(true, false);
			volume_filter[4]->setToggleState(false, false);
			if( volume_filter[0] )
				volume_filter[0]->setToggleState(false, false);
			if( volume_filter[1] )
				volume_filter[1]->setToggleState(false, false);
			if( volume_filter[3] )
				volume_filter[3]->setToggleState(false, false);
			File folder("e:");
			directoryList->setDirectory(folder, true, true);
			directoryList->refresh();
		}
		else if( buttonThatWasClicked == volume_filter[3] )
		{
			volume_filter[3]->setToggleState(true, false);
			volume_filter[4]->setToggleState(false, false);
			if( volume_filter[0] )
				volume_filter[0]->setToggleState(false, false);
			if( volume_filter[1] )
				volume_filter[1]->setToggleState(false, false);
			if( volume_filter[2] )
				volume_filter[2]->setToggleState(false, false);
			File folder("f:");
			directoryList->setDirectory(folder, true, true);
			directoryList->refresh();
		}
	}

	void selectionChanged() { }
	void fileClicked(const File &file, const MouseEvent &e) { }
	void browserRootChanged(const File &newRoot) { }
	void fileDoubleClicked(const File &file);


private:
    ScopedPointer<FileTreeComponent>			fileTreeComp;
    ScopedPointer<DirectoryContentsList>		directoryList;
	ScopedPointer<ModelAndTextureFileFilter>	filter;
	ScopedPointer<TextButton>					model_filter[4];
	ScopedPointer<TextButton>					volume_filter[5];
	ScopedPointer<TextButton>					texture_switch;
    TimeSliceThread								thread;
	ScopedPointer<TextureViewComponent>			TextureViewDlg;
	ModelEditorMainFrame*						pMainFrame;
};

#endif